﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using MichMan.Utilities;

namespace MichMan.Utilities.UnitTest
{

    [TestClass]
    public class ObjectUtiltiesTests
    {
        [TestMethod]
        public void TestOnce()
        {
            Once once = new Once();
            Assert.IsTrue(once.Value);
            Assert.IsFalse(once.Value);
            Assert.IsTrue(once.Predicate());

            once = new Once();
            Assert.IsTrue(once.Predicate());
            once.Set();
            Assert.IsFalse(once.Value);
        }

        [TestMethod]
        public void TestChanged()
        {
            int i = 0;
            Changed<int> changed = new Changed<int>(() => i);
            Assert.IsTrue(changed.Predicate() == 0);
            Assert.IsTrue(changed.Value);
            Assert.IsFalse(changed.Value);
            i = 5;
            Assert.IsTrue(changed.Value);
            Assert.IsFalse(changed.Value);
            Assert.IsTrue(changed.Predicate() == 5);

        }

        [TestMethod]
        public void TestGetter()
        {
            Getter<object> getter = new Getter<object>(() => new object());
            Assert.IsFalse(getter.Has);
            Assert.IsNotNull(getter.Value());
            Assert.IsTrue(getter.Has);
        }

        [TestMethod]
        public void TestSingleton()
        {
            Assert.IsNull(Singleton<ObjectUtiltiesTests>.Get());
            Assert.IsNotNull(Singleton<ObjectUtiltiesTests>.Create(() => new ObjectUtiltiesTests()));
            Assert.IsNotNull(Singleton<ObjectUtiltiesTests>.Get());

            ObjectUtiltiesTests obj = new ObjectUtiltiesTests();
            Singleton<ObjectUtiltiesTests>.Set(obj);
            Assert.ReferenceEquals(obj, Singleton<ObjectUtiltiesTests>.Get());
            Assert.ReferenceEquals(obj, Singleton<ObjectUtiltiesTests>.Create(() => new ObjectUtiltiesTests()));
        }
    }
}